﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace block_explorer.Shaders
{
    class CombineEffect
    {
        private const string sEffectFilename = "Effects/Combine";
        private const string sHalfPixelParameterName = "HalfPixel";
        private const string sColorMapParameterName = "ColorMap";
        private const string sLightMapParameterName = "LightMap";
        private const string sDefaultTechniqueName = "Combine";

        private Effect mEffect;

        private EffectParameter mHalfPixel;
        private EffectParameter mColorMap;
        private EffectParameter mLightMap;

        private Vector2 HalfPixel { set { mHalfPixel.SetValue(value); } }
        private Texture ColorMap { set { mColorMap.SetValue(value); } }
        private Texture LightMap { set { mLightMap.SetValue(value); } }

        public CombineEffect(GraphicsDevice device, ContentManager content)
        {
            mEffect = content.Load<Effect>(sEffectFilename);

            mHalfPixel = mEffect.Parameters[sHalfPixelParameterName];
            mColorMap = mEffect.Parameters[sColorMapParameterName];
            mLightMap = mEffect.Parameters[sLightMapParameterName];

            mEffect.CurrentTechnique = mEffect.Techniques[sDefaultTechniqueName];
        }

        public void Combine(Texture2D colorMap, Texture2D lightMap, Vector2 halfPixel)
        {
            ColorMap = colorMap;
            LightMap = lightMap;
            HalfPixel = halfPixel;

            mEffect.CurrentTechnique.Passes[0].Apply();
            Util.QuadRenderComponent.Instance.Render(-1 * Vector2.One, Vector2.One);
        }
    }
}
